extends "../cTDW/cTDW.gd"
func _info():
	pass

func _connect():
	._connect()

var norcnt:int = 0
var tlv = 0
var nlv = 0
var dragonon = 1
var spw1 = 0.6
var spw2 = 1.08
var spw3 = 0.06

func _extInit():
	._extInit()
	#chaName = "圣剑客·Lv[0]"
	lv = 4
	attCoe.atkRan = 2
	attCoe.atk = 40 / 8.5 * 2 / 4
	attCoe.maxHp = 550 / 112.5 / 4
	addCdSkill("yxlqs",5)
	addCdSkill("yxlqs1",11)
	addSkillTxt("龙之伙伴：开局在身边召唤契约魔法龙[身边没有位置则召唤失败，龙随着英雄等级增加而变强]")
	addSkillTxt("龙枪舞击：挥舞长枪，对自身九宫格范围内敌人造成<（120+技能等级*2）%物强>点伤害[cd:5秒][color=#666666][未解锁][/color]")
	addSkillTxt("龙魂击：当契约龙在场时，普攻3次[击中]后将触发龙魂击【对当前敌人造成其已损失血量<20+技能等级>%魔法伤害】[color=#666666][未解锁][/color]")
	addSkillTxt("龙血浴身：当契约龙在时，消耗其<6-技能等级>%当前血量，为英雄增加20%增伤和33%攻速，持续6秒[cd:11秒][color=#666666][未解锁][/color]")
	addSkillTxt("[color=red]Tips:杀人会获得经验，战斗结束后会计算经验并升级;升级获得的属性和解锁的技能及等级全局有效")
	nlv = 0
	lvup()
	if skill3unlock:
		skillunlock(3)
	if skill2unlock:
		skillunlock(2)
	if skill1unlock:
		skillunlock(1)

func _castCdSkill(id):
	if id == "yxlqs" && skill1unlock:
		cst("龙枪舞击","000000",position)
		var chas = getAllChas(1)
		for i in chas:
			if abs(i.cell.x - cell.x) <= 1 && abs(i.cell.y - cell.y ) <= 1:
				hurtChara(i,(1.2 + 0.02*skill1lv)*att.atk,Chara.HurtType.PHY,Chara.AtkType.SKILL)

	if id == "yxlqs1" && skill3unlock:
		skill3up()
		var chas = getAllChas(2)
		for i in chas:
			if i.id.find("dragon") != -1:
				cst("龙血浴身","000000",position)
				i.plusHp(-att.hp * spw3)
				addBuff(LBF.new(1))
				break

func _onBattleStart():
	._onBattleStart()
	norcnt = 0
	dragonon = 1
	var drid = "cex___Hide_dragon"
	if nlv <= 5:
		drid += "1"
	elif nlv <= 10:
		drid += "2"
	elif nlv <= 15:
		drid += "3"
	else:
		drid += "4"
	newChara(drid,self.cell)
	if sys.test:
		lvup()
		if !skill3unlock:
			#print("unlock3")
			skillunlock(3)

func _onCharaDel(cha):
	if cha.id.find("dragon") != -1 && cha.team == self.team:
		dragonon = 0

func _onAtkChara(atkinfo):
	if atkinfo.atkType == Chara.AtkType.NORMAL && !atkinfo.isMiss && atkinfo.hitCha != self && atkinfo.atkCha == self:
		norcnt += 1
		if norcnt % 3 == 0 && dragonon && skill2unlock:
			skill2up()
			cst("龙魂击","000000",position)
			var dmg = atkinfo.hitCha.att.maxHp -  atkinfo.hitCha.att.hp
			dmg *= spw2
			hurtChara(atkinfo.hitCha,dmg,Chara.HurtType.MGI,Chara.AtkType.SKILL)
	._onAtkChara(atkinfo)

func _onBattleEnd():
	._onBattleEnd()
	yield(reTimer(0.01),"timeout")
	lvup()
	upAtt()

func lvup():
	#killNum
	#	print(killNum)
	#if chaName.find("Lv[") == -1:
	#	nlv = 0
	#else:
	#	nlv = int(chaName.right(chaName.find("[")).left(2))
	#	print(nlv)
	if killNum >= 2:
		tlv = 1
	var e = 2
	var k = 3
	while(killNum >= e + k):
		tlv += 1
		e = e + k
		k += 1
	if sys.test:tlv = nlv + 1
	tlv = min(18,tlv)
	if tlv > nlv:
		upattadd(tlv,nlv)
		nlv = tlv
	chaName = "龙骑士·Lv[" + str(nlv) + "]"
		#print(chaName)

func upattadd(tlv,nlv):
	attAdd.atk += (tlv - nlv) * 8
	attAdd.maxHp += (tlv - nlv) * 50
	attAdd.def += (tlv - nlv) * 6
	attAdd.mgiDef += (tlv - nlv) * 6
	attAdd.spd += (tlv - nlv) * 0.03
	healCha(self,(tlv - nlv) * 20)

func skill1up():
	spw1 *=(1 + skill1lv * 0.09 + 0.12)

func skill2up():
	spw2 = 0.2 + skill2lv * 0.01

func skill3up():
	spw3 = 0.1 - skill3lv * 0.01 

class LBF:
	extends Buff
	func _init(lv):
		attInit()
		life = lv * 6
		id == "DragonBuff"
		att.atkR = 0.2
		att.spd = 0.33